<?php

namespace pmvc\mvc\controller;

use pmvc\ioc\ObjectFactory;
use pmvc\ioc\InitializingObject;
use pmvc\mvc\ControllerMapping;
use pmvc\mvc\Controller;
use pmvc\mvc\Request;
use pmvc\util\ClassUtil;
use pmvc\log\Log;
use Exception;

class ObjectNameControllerMapping 
	implements InitializingObject,
	ControllerMapping {

	private $objectFactory;

	public function initialize(ObjectFactory $objectFactory) {
		$this->objectFactory = $objectFactory;
	}

	public function getController($url, Request $request) {
		if ($this->objectFactory->hasObjectWithName($url, true)) {
			$obj = $this->objectFactory->getObjectByName($url);
			if (!ClassUtil::isInstance($obj, 'pmvc\mvc\Controller')) {
				Log::error("Object with name {} is not a Controller", $url);
				throw new Exception("Object is not an instance of Controller");
			}
			return $obj;
		}
		return NULL;
	}
}


?>